package cn.sxt.sys.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.sxt.sys.constast.SYS_Constast;
import cn.sxt.sys.domain.Dept;
import cn.sxt.sys.service.DeptService;
import cn.sxt.sys.utils.DataGridView;
import cn.sxt.sys.utils.TreeNode;
import cn.sxt.sys.vo.DeptVo;

@Controller
@RequestMapping("dept")
public class DeptController {

	@Autowired
	private DeptService deptService;

	// 跳到deptManager.jsp
	@RequestMapping("toDeptManager")
	public String toDeptManager() {
		return "system/dept/deptManager";
	}

	// deptManager.jsp加载 左边
	@RequestMapping("toDeptLeft")
	public String toDeptLeft() {
		return "system/dept/deptLeft";
	}

	// deptManager.jsp加载 右边
	@RequestMapping("toDeptRight")
	public String toDeptRight() {
		return "system/dept/deptRight";
	}

	// 加载左边 部门树
	@RequestMapping("loadDeptTree")
	@ResponseBody
	public List<TreeNode> loadDeptTree(DeptVo deptVo) {
		List<TreeNode> treeNodes = new ArrayList<>();
		List<Dept> depts = deptService.queryAllDeptForList(deptVo);
		for (Dept d : depts) {
			Boolean open = d.getOpen() == SYS_Constast.TYPE_PUBLIC_ONE ? true : false;
			Boolean isParent = d.getParent() == SYS_Constast.TYPE_PUBLIC_ONE ? true : false;
			treeNodes.add(new TreeNode(d.getId(), d.getPid(), d.getName(), open, isParent));
		}
		return treeNodes;
	}

	// 部门数据，分页，模糊
	@RequestMapping("queryAllDept")
	@ResponseBody
	public DataGridView queryAllDept(DeptVo deptVo) {
		return deptService.queryAllDept(deptVo);
	}

	// 跳添加页面
	@RequestMapping("toAddDept")
	public String toAddDept() {
		return "system/dept/deptAdd";
	}

	// 添加
	@RequestMapping("addDept")
	@ResponseBody
	public Map<String, Object> addDept(DeptVo deptVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = deptService.addDept(deptVo);
			if (i > 0) {
				msg = "添加成功";
			} else {
				msg = "添加失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "添加失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 跳修改页面，查回显信息
	@RequestMapping("toUpdateDept")
	public String toUpdateDept(DeptVo deptVo, Model model) {
		Dept dept = deptService.queryDeptById(deptVo.getId());
		model.addAttribute("dept", dept);
		return "system/dept/deptUpdate";
	}

	// 修改
	@RequestMapping("updateDept")
	@ResponseBody
	public Map<String, Object> updateDept(DeptVo deptVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = deptService.updateDept(deptVo);
			if (i > 0) {
				msg = "修改成功";
			} else {
				msg = "修改失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "修改失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 删除
	@RequestMapping("deleteDept")
	@ResponseBody
	public Map<String, Object> deleteDept(DeptVo deptVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = deptService.deleteDeptById(deptVo);
			if (i > 0) {
				msg = "删除成功";
			} else {
				msg = "删除失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "删除失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 批量删除日志
	@RequestMapping("batchDeleteDept")
	@ResponseBody
	public Map<String, Object> batchDeleteDept(DeptVo deptVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			Integer[] ids = deptVo.getIds();
			if (ids != null && ids.length > 0) {
				for (int i = 0; i < ids.length; i++) {
					deptVo.setId(ids[i]);
					int j = deptService.deleteDeptById(deptVo);
					if (j > 0) {
						msg = "删除成功";
					} else {
						msg = "删除失败";
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "删除失败";
		}
		map.put("msg", msg);
		return map;
	}

}
